<?php

namespace app\api\command;

use app\common\model\Packages;
use app\common\model\User;
use fast\Random;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class Orderdailimore extends Command
{
    protected function configure()
    {
        $this->setName('order_daili_more')->setHelp('平台1二次结算')->setDescription('平台1二次结算');

    }

    protected function execute(Input $input, Output $output)
    {
        $orderModel             = new \app\common\model\Order();
        $userModel              = new User();
        $where['platform_type'] = 1;
        $where['is_more']       = 2;
        $where['is_agency']     = 2;
        $where['more_num']      = 1;
        $where['order_status']  = 5;
//        $where['audit_status']  = 2;
        $order                  = $orderModel->where($where)->order('id desc')->select();
//        dump($order);exit();
        if ($order) {
            $url = "http://agent.haoka.bx10086.cn/v1/haoka_api/orders";//办理套餐
            foreach ($order as $k => $v) {
                sleep(2);
                $data['appid']     = config('site.ooen_appid');
                $data['timestamp'] = time();
                $data['nonce_str'] = Random::alnum(32);
                $data['order_id']  = $v['order_id'];
                $data['phone']     = $v['shipping_phone'];
                $string            = ascll($data);
                $stringSignTemp    = $string.'&appsecret='.config('site.open_appsecret');
                $data['sign']      = mb_strtoupper(md5($stringSignTemp));
                $res               = httpPost($url, $data);
                $arr               = json_decode($res, true);
                unset($data);
//                $arr['rows'][0]['remark'] = '100';
//                $arr['rows'][0]['rebate_num'] = 2;
                if (!empty($arr['rows'][0])) {
                    $rows = $arr['rows'][0];
                    if (strpos($rows['remark'], '100') !== false || strpos($rows['remark'], '可结算')) {
                        if($v->audit_status == 2){
                            if (isset($rows['rebate_num']) && $rows['rebate_num'] != '') {
                                if ($rows['rebate_num'] == 2) {
                                    $packageInfo        = Packages::where(['id' => $v['goods_id']])->find();
                                    $v->settlement_time = time();
                                    $v->is_more         = 3;
                                    $v->is_hexiao       = 8;
                                    $v->more_num        = $rows['rebate_num'];
                                    $v->baoxiao_status  = 2;
                                    $v->save();
                                    $huafei            = $packageInfo['ciyue_set_meal'];
                                    if ($huafei) {
                                        $userModel::money($huafei, $v['user_id'], '话费报销', $v['id'],
                                            $packageInfo['id']);
                                    }
                                    jiangli($v, $packageInfo, 1, $v->more_num);
                                    if ($v['parent_id']) {
                                        jiangli($v, $packageInfo, 2, $v->more_num);
                                    }
                                    if ($v['two_id']) {
                                        jiangli($v, $packageInfo, 3, $v->more_num);
                                    }
                                    //区县
                                    if ($v['agency_area_id']) {
                                        jiangli($v, $packageInfo, 4, $v->more_num);
                                    }
                                    //小区
                                    if ($v['plot_id']) {
                                        jiangli($v, $packageInfo, 5, $v->more_num);
                                    }
                                }
                            }
                        }else{
                            $v->is_hexiao  = 8;
                            $v->save();
                        }

                    }
                }
                echo "操作成功";
            }
        }
    }


}
